<?php
/**
 * Created by PhpStorm.
 * User: yyc
 * Date: 2018/7/24
 * Time: 9:54
 */
class OAApprovalModelController extends BaseController
{
    /**
     * @var object 验证方法对象
     */
    protected $validator;
    /**
     * @var array 返回数据模板
     */
    protected $response = ['code' => 0, 'msg' => 'ok'];
    /**
     * @var object 用户信息
     */
    protected $user;

    /**
     * 用于鉴定用户的token
     * OAApprovalModelController constructor.
     */
    public function __construct()
    {
        header('Access-Control-Allow-Origin:*');	//解决资源跨域问题
	header('Access-Control-Allow-Credentials', 'true');
        
        $this->user = $this->verificationOAUserToken(Input::get('token'), $this->response);
    }

    /**
     *
     * @Author:      yyc
     * @DateTime:    18-7-25 上午11:00
     * @Description: 验证用户token
     * @return string
     * @link http://api.anhui.com/oa/approval/index?token=cbaeef3465cf329e7167fedb847fb11b
     */
    public function index()
    {
        $this->validator = Validator::make(Input::all(), ['token' => 'required']);
        do {
            //验证token
            if ($this->validator->fails()) {
                $this->response = Lang::get('oa_error.args_error_100000');
                $this->response += ['result' => $this->validator->messages()];
                break;
            }
            //验证用户
            if (!$this->user) {
                break;
            }

            //获取个人可以用的审批模板和最近常用的审批模板
            $this->response = [
                'code' => 0,
                'msg' => 'ok',
                'resultUsually' => OAApprovalModel::usuallyType($this->user->id),
                'resultSelfInfo' => OAApprovalModel::approvalInfo($this->user->id)
            ];
        } while (false);

        //返回数据
        Log::info('info', [
            'system' => 'oa',
            'event' => 'approval/index',
            'input' => Input::all(),
            'output' => $this->response
        ]);

        return json_encode($this->response);
    }

    /**
     * 用于审批设置页面列表和查询
     * @Author:      yyc
     * @DateTime:    18-7-25 上午11:00
     * @Description: 验证用户token
     * @return string
     * @link http://api.anhui.com/oa/approval/searchList?token=cbaeef3465cf329e7167fedb847fb11b
     */
    public function searchList()
    {
        $this->validator = Validator::make(Input::all(), [
            'token' => 'required',
            'page' => 'integer',
            'page_size' => 'integer',
        ]);
        do {
            //验证token
            if ($this->validator->fails()) {
                $this->response = Lang::get('oa_error.args_error_100000');
                $this->response += ['result' => $this->validator->messages()];
                break;
            }
            //验证用户
            if (!$this->user) {
                break;
            }

            $type       = Input::get('type', '');               //审批类型
            $status     = Input::get('status', [1, 2]);          //审批状态
            $page       = Input::get('page', 1);                //页码 [默认 1]
            $pageSize   = Input::get('page_size', 10);          //每页显示数据条数 [默认 10]

            //获取个人可以用的审批模板和最近常用的审批模板
            $this->response = [
                'code' => 0,
                'msg' => 'ok',
                'result' => [
                    'type' => OAApprovalModel::typeNeedAll(),
                    'info' => OAApprovalModel::searchInfo($status, $page, $pageSize, $type),
                ],
            ];
        } while (false);

        //返回数据
        Log::info('info', [
            'system' => 'oa',
            'event' => 'approval/searchList',
            'input' => Input::all(),
            'output' => $this->response
        ]);

        return json_encode($this->response);
    }


    /**
     * 用于审批状态的改变
     * @Author:         yyc
     * @DateTime:       18-7-26 上午11:00
     * @Description:    验证用户token
     * @param           token
     * @param           status              int     修改的状态
     * @param           modelId             int     审批模板的ID
     * @link http://api.anhui.com/oa/approval/openStartStatus?token=cbaeef3465cf329e7167fedb847fb11b
     * @return string
     */
    public function openStartStatus()
    {
        $this->validator = Validator::make(Input::all(), [
            'token' => 'required',
            'status' => 'integer|required|between:1,3',
            'modelId' => 'integer|required'
        ]);
        do {
            //验证token
            if ($this->validator->fails()) {
                $this->response = Lang::get('oa_error.args_error_100000');
                $this->response += ['result' => $this->validator->messages()];
                break;
            }
            //验证用户
            if (!$this->user) {
                break;
            }

            $status     = Input::get('status', '');             //审批状态
            $modelId    = Input::get('modelId', '');            //每页显示数据条数 [默认 10]
            $old_data   = json_encode(OAApprovalModel::find($modelId));
            $statusDesc = [
                1 => '启用模板状态',
                2 => '停用模板状态',
                3 => '删除模板状态'
            ];
            try {
                OAApprovalModel::changeStatus($status, $modelId, $this->user->id);
            } catch (Exception $e) {
                Log::info('info', [
                    'system' => 'oa',
                    'event' => 'approval/openStartStatus',
                    'input' => Input::all(),
                    'exception' => $e
                ]);
                $this->response = Lang::get('oa_error.approval_model_status_error_212002');
                break;
            }

            try {
                OAOpLog::writeLog(
                    'approval/openStartStatus', 2,
                    $statusDesc[$status], $old_data, json_encode(OAApprovalModel::find($modelId)),
                    $this->user->id
                );
            } catch (Exception $e) {
                Log::info('info', ['system' => 'oa', 'event' => 'approval/openStartStatus', 'input' => Input::all(), 'exception' => $e]);
                $this->response = Lang::get('oa_error.new_log_write_error_212001');
                break;
            }

        } while (false);

        //返回数据
        Log::info('info', [
            'system' => 'oa',
            'event' => 'approval/openStartStatus',
            'input' => Input::all(),
            'output' => $this->response
        ]);

        return json_encode($this->response);

    }

}